#include "MainApp.h"

MainApp::MainApp(void)
{
}

MainApp::~MainApp(void)
{
}

int MainApp::initGL(void)
{
	initGLExtensions();

	glShadeModel(GL_SMOOTH);							// Enable Smooth Shading
	glClearColor(0.5f, 0.5f, 0.5f, 1.0f);				// White Background
	glClearDepth(1.0f);									// Depth Buffer Setup
	glEnable(GL_DEPTH_TEST);							// Enables Depth Testing
	glDepthFunc(GL_LEQUAL);								// The Type Of Depth Testing To Do
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	// Really Nice Perspective Calculations
	return TRUE;										// Initialization Went OK
}


void MainApp::resizeGLWindow(int width, int height)
{
	m_screenW = width;
	m_screenH = height;

	if (m_screenH==0)										// Prevent A Divide By Zero By
	{
		m_screenH=1;										// Making Height Equal One
	}

	m_AspectRatio = (GLfloat)m_screenW/(GLfloat)m_screenH;

	glViewport(0,0,m_screenW,m_screenH);						// Reset The Current Viewport
}


int MainApp::initScene(void)
{
	m_Stride = 2;
	m_NumVerts = 4;
	m_NumFloats = m_NumVerts * m_Stride;
	m_verts = new float[m_NumFloats];
	m_verts[0] = -1; m_verts[1] = -1; 
	m_verts[2] = 1; m_verts[3] = -1;
	m_verts[4] = 1; m_verts[5] = 1;
	m_verts[6] = -1; m_verts[7] = 1;

	// create VBOs
	glGenBuffers( 1, &m_vertsVBO );
	glBindBuffer(GL_ARRAY_BUFFER, m_vertsVBO);
	glBufferData(
		GL_ARRAY_BUFFER, 
		m_NumFloats * sizeof(GLfloat), 
		m_verts, 
		GL_STATIC_DRAW);

	// unbind VBOs
	glBindBuffer(GL_ARRAY_BUFFER, 0);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

	m_indices = new GLuint[4];
	for (GLuint i = 0; i < 4; i++)
		m_indices[i] = i;

	return 0;
}

int MainApp::renderScene(void)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear Screen And Depth Buffer

	glMatrixMode(GL_PROJECTION);							// Select The Projection Matrix
	glLoadIdentity();										// Reset The Projection Matrix
	glOrtho(-m_AspectRatio,m_AspectRatio,-1.0f,1.0f,-1.0f,1.0f);	
	glMatrixMode(GL_MODELVIEW);								// Select The Modelview Matrix
	glLoadIdentity();										// Reset The Modelview Matrix

	// Enable Pointers
	glEnableClientState( GL_VERTEX_ARRAY );	
	//glEnableClientState( GL_NORMAL_ARRAY );	
	//glEnableClientState( GL_TEXTURE_COORD_ARRAY );

	// Bind VBOs
	glBindBuffer( GL_ARRAY_BUFFER_ARB, m_vertsVBO );
	glVertexPointer(  2, GL_FLOAT, m_Stride*sizeof(float), (GLvoid*)(0)			   );					
	//glNormalPointer( 	 GL_FLOAT, m_Stride*sizeof(float), (GLvoid*)(3*sizeof(float)) );
	//glTexCoordPointer(2, GL_FLOAT, m_Stride*sizeof(float), (GLvoid*)(UVoffset*sizeof(float)) );
	//glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, matGrpGL->m_indicesVBO );

	//glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
	glDrawElements( GL_QUADS, 4, GL_UNSIGNED_INT, m_indices );

	// Unbind VBOs
	glBindBuffer( GL_ARRAY_BUFFER_ARB, 0 );
	//glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );

	// Disable Pointers
	glDisableClientState( GL_VERTEX_ARRAY );			
	//glDisableClientState( GL_NORMAL_ARRAY );			
	//glDisableClientState( GL_TEXTURE_COORD_ARRAY );

	return 0;
}
